<template>
<div class="discount-wrap">
    <div class="header fixed">
        <!-- <van-nav-bar title="折扣商品" left-text="返回" right-text="搜索" left-arrow @click-left="$router.go(-1)" @click-right="searchType = !searchType">
          <van-icon slot="right" name="search" />
        </van-nav-bar> -->
        <van-nav-bar title="無优周末" left-text="返回"  left-arrow @click-left="$router.go(-1)" />
        <transition name="van-slide-down">
          <van-search placeholder="请输入搜索关键词" v-show="searchType" v-model="searchValue" show-action >
            <div slot="action" @click="onSearch">搜索</div>
          </van-search>
        </transition>
    </div>

   <div class="header-swipe">
        <van-swipe :autoplay="3000">
            <van-swipe-item v-for="(data, index) in swipeImage" :key="index">
                <img class="swipeimage" :ref="data.image" style="width: 100%;" :src="data.image" />
            </van-swipe-item>
        </van-swipe>
    </div>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="getGoodsData">
        <Goods :data="aGoodsData" :type="0" :load="false" @getDetails="getDetails" />
    </van-list>
    <copyright />
</div>
</template>
<script>
import Goods from '@/common/goods/';
export default {
    name: 'Discount',
    data() {
        return {
            aGoodsData: null,
            c_page: 1,
            l_page: null,
            loading: true,
            finished: false,
            searchType: false,
            searchValue: null,
            swipeImage: []
        }
    },
    components: {
        Goods
    },
    created() {
        this.getGoodsData()
        this.swipeImageData()
    },
    methods: {
        swipeImageData(){
            let params = {
                token: sessionStorage.getItem('token'),
            }
            this.axios.get('/api/store/user/swiper/discountSwiper',{params}).then(res=>{
                this.swipeImage = res.data.data;
            }).catch(err=>{
                console.log(err);
            })
        },
        getGoodsData() {
            let params = {
                token: sessionStorage.getItem('token'),
                page: this.c_page
            };
            this.axios.get('/api/store/official/discount', {
                params
            }).then(r => {
                if (this.c_page <= r.data.data.last_page) {
                    this.loading = true;
                    if (r.data.data.current_page == 1) {
                        this.aGoodsData = r.data.data.data;
                        this.loading = false;
                    } else {
                        this.aGoodsData = [...this.aGoodsData, ...r.data.data.data];
                        this.loading = false;
                    }
                } else {
                    this.finished = true;
                    this.loading = false;
                }
                this.c_page = r.data.data.current_page + 1;
                this.l_page = r.data.data.last_page;
            }).catch(e => console.log(e))
        },
        onSearch() {
          let params= {
            token: sessionStorage.getItem('token'),
            keywords: this.searchValue,
          }
          this.axios.get('/api/store/official/discount',{params}).then(r=>{
            console.log(r);
          }).catch(err=>{
            console.log(err);
          })
        },
        getDetails(e) {
            this.global.state.productStore_goods = e;
            this.$router.push({
                name: 'productStore',
                path: './product/store'
            })
        },
    }
}
</script>

<style lang="less" scoped>
.discount-wrap {
    padding: 50px 0 20px;

    .header {
        top: 0;
        left: 0;
        right: 0;
    }
}
</style>
